package listbyorder.access101_200.test154;

/**
 * @author code_yc
 * @version 1.0
 * @date 2020/6/13 10:15
 */
public class Solution1 {

    // 寻找旋转排序数组中的最小值 II
    public int findMin(int[] nums) {
        if (nums.length == 1) return nums[0];
        int s = 0;
        int e = nums.length - 1;
        while (s < e) {
            int mid = (e + s) >>> 1;
            if (nums[mid] > nums[e]) {
                s = mid + 1;
            } else if (nums[mid] < nums[e]) {
                e = mid;
            } else {
                int temp = mid;
                while (temp >= 0 && nums[temp] == nums[mid]) {
                    temp--;
                }
                if (temp >= 0) {
                    if (nums[temp] < nums[mid]) {
                        e = mid;
                    } else if (nums[temp] == nums[mid]) {
                        s = mid + 1;
                    } else {
                        return nums[mid];
                    }
                } else {
                    s = mid + 1;
                }
            }
        }
        return nums[s];
    }
}
